package com.v5ba;

import cn.hutool.core.io.file.FileWriter;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;

import java.text.MessageFormat;
import java.util.List;

public class ReadExcelToTxt {
    public static String headStr = "insert into tab1(col1, col2) values ";
    // 示例: "(''{0}'', {1}, {2}, {3}),";
    public static String formatStr = "(''{0}'', {1}, {2}, {3}),";
    public static String excelPath = "/Users/zhangyanhui/Desktop/data.xlsx";
    public static String targetPath = "/Users/zhangyanhui/Desktop/db/a.sql";
    public static void toFile(){
        StringBuilder stringBuilder =  new StringBuilder(headStr);
        ExcelReader reader = ExcelUtil.getReader(excelPath);
        List<List<Object>> list = reader.read(1);
        for (List<Object> row : list){
            int len = row.size();
            Object[] param = new Object[len];
            for (int i = 0; i < len; i++){
                param[i] = row.get(i);
            }
            stringBuilder.append(Constant.ROW_END_STRING).append(MessageFormat.format(formatStr, param));
        }
        FileWriter writer = new FileWriter(targetPath);
        writer.write(stringBuilder.toString());
        System.out.println("读取"+list.size()+"条数据");
    }
    public static void main(String[] args) {
        toFile();
    }
}
